package com.quanquan.management.service;

import DTO.PageDTO;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.quanquan.management.dto.ConsumptionQuotaDTO;
import com.quanquan.management.request.ConsumptionQuotaRequestBody;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import utils.Request;

import java.util.LinkedList;
import java.util.List;

/**
 * 功能描述:
 *
 * @ClassName: ConsumptionQuotaService
 * @Author: xiaoqianh丶
 * @Date: 2019-06-12 09:56
 * @Version: V1.0
 */
@Service
public class ConsumptionQuotaService {
    @Value("${module-url.userUrl}")
    private String userUrl;
    /**
     * 列表
     * @param request
     * @return
     */
    public PageDTO<ConsumptionQuotaDTO> list(ConsumptionQuotaRequestBody request) {
        String json = Request.postJson(userUrl+"consumption_quota/query", request);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        PageDTO<ConsumptionQuotaDTO> pageDTO = null;
        if (0 == resCode) {

            JSONObject jsonData = jsonObject.getJSONObject("data");
            PageDTO data = jsonObject.getJSONObject("data").toJavaObject(PageDTO.class);
            JSONArray list = jsonData.getJSONArray("list");
            List<ConsumptionQuotaDTO> consumptionAmountDTOS = new LinkedList<>();
            for (int i = 0; i < list.size(); i++ ) {
                ConsumptionQuotaDTO object = list.getObject(i, ConsumptionQuotaDTO.class);
                consumptionAmountDTOS.add(object);
            }
            data.setList(consumptionAmountDTOS);
            return data;
        }
        return pageDTO;
    }


    public boolean addConsumptionQuota(ConsumptionQuotaRequestBody requestBody) {

        if (null == requestBody) {
            return false;
        }
        String json = Request.postJson(userUrl + "consumption_quota/add", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }


    public ConsumptionQuotaDTO findById(String id) {
        ConsumptionQuotaRequestBody requestBody = new ConsumptionQuotaRequestBody();
        requestBody.setConsumptionId(id);
        String json = Request.postJson(userUrl + "consumption_quota/query_by_id", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        ConsumptionQuotaDTO consumptionQuotaDTO = null;
        if (0 == resCode) {
            JSONObject data = jsonObject.getJSONObject("data");
            consumptionQuotaDTO = data.toJavaObject(ConsumptionQuotaDTO.class);
        }
        return consumptionQuotaDTO;
    }



    public boolean update(ConsumptionQuotaRequestBody requestBody) {
        if (null == requestBody) {
            return false;
        }
        String marketingRule = requestBody.getMarketingRule();

        String json = Request.postJson(userUrl + "consumption_quota/update", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        if (0 == resCode) {
            return true;
        }
        return false;
    }

    public boolean delete(String id) {
        ConsumptionQuotaRequestBody requestBody = new ConsumptionQuotaRequestBody();
        requestBody.setConsumptionId(id);
        String json = Request.postJson(userUrl + "consumption_quota/remove", requestBody);
        JSONObject jsonObject = JSONObject.parseObject(json);
        Integer resCode = jsonObject.getInteger("resCode");
        return resCode == 0;
    }

}
